package org.bluedream.core.module.service.bill;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import org.bluedream.comm.utils.CreateGUIDUtil;
import org.bluedream.comm.utils.EmptyUtil;
import org.bluedream.core.module.base.OrgBaseServiceImpl;
import org.bluedream.core.module.dao.bill.OddImportDao;
import org.bluedream.core.module.entity.bill.OddImport;
import org.bluedream.core.utils.DictUtil;
import org.bluedream.core.utils.UserUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.text.SimpleDateFormat;

/**
 * @ClassName OddImportService
 * @Description TODO
 * @Author foxsand
 * @Data 2021-09-09 9:44
 * @Version
 */
@Service
@DS("master")
@Transactional(
        readOnly = true
)
public class OddImportService extends OrgBaseServiceImpl<OddImportDao, OddImport> implements IService<OddImport> {
    private static final Logger logger = LoggerFactory.getLogger(OddImportService.class);

    @Override
    public OddImport getById(Serializable id) {
        if (EmptyUtil.isEmpty(id)){
            return new OddImport();
        }
        return super.getById(id);
    }

    @Transactional(
            readOnly = false,
            rollbackFor = Exception.class
    )
    public boolean save(String billType , String lastNum) {
        java.util.Date fileDate = new java.util.Date();
        QueryWrapper<OddImport> qw = new QueryWrapper<>();
        qw.eq("type" , billType);
        qw.eq("org_code" , UserUtil.getLoginOrgCode());
        OddImport oddImport = EmptyUtil.isNoEmpty(super.getOne(qw))?super.getOne(qw):new OddImport();
        oddImport.setType(billType);
        SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd");
        java.sql.Date sDate = java.sql.Date.valueOf(df.format(fileDate));
        oddImport.setFileDate(sDate);
        oddImport.setLastNum(lastNum);
        return super.save(oddImport);
    }

    public String getNowBillNum(String type){
        java.util.Date date = new java.util.Date();
        SimpleDateFormat df = new SimpleDateFormat("YYYY-MM-dd");
        java.sql.Date sDate = java.sql.Date.valueOf(df.format(date));
        QueryWrapper<OddImport> qw = new QueryWrapper<>();
        qw.eq("type" , type);
        qw.eq("fileDate" , sDate);
        qw.eq("org_code" , UserUtil.getLoginOrgCode());
        OddImport oddImport = super.getOne(qw);

        if (EmptyUtil.isNoEmpty(oddImport)){
            return CreateGUIDUtil.nextCode(oddImport.getLastNum());
        }else {
            StringBuffer sb = new StringBuffer();
            int billLength;
            if (EmptyUtil.isNoEmpty(DictUtil.getDictValue("sys.bill.length", type, ""))) {
                billLength = Integer.parseInt(DictUtil.getDictValue("sys.bill.length", type, ""));
            } else {
                billLength = Integer.parseInt(DictUtil.getDictValue("sys.bill.length", "default", ""));
            }
            for (int i = 0; i < (billLength - 1); i++) {
                sb.append("0");
            }
            sb.append("1");
            df = new SimpleDateFormat("yyyyMMdd");
            return type + df.format(date) + sb.toString();
        }
    }
}
